package com.tuanzi.loan.web.bean.settlementsystem;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementCompany;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementIncome;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementIncomeActivity;
import com.tuanzi.loan.business.entity.settlementsystem.SettlementPrepay;
import com.tuanzi.loan.business.service.settlementsystem.*;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.settlementsystem.SettlementRemainRequest;
import com.tuanzi.loan.vo.response.settlementsystem.SettlementRemainResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.settlementsystem.SettlementRemainLazyDataModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.util.List;
import java.util.stream.Collectors;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

/**
 * @author caiwanlin
 * @create 2018-08-22 17:52
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class SettlementRemainListBean extends JSFListingBean<SettlementRemainRequest,SettlementRemainResponse> {
    @Autowired
    private SettlementCompanyService settlementCompanyService;

    @Autowired
    private SettlementRemainService settlementRemainService;

    @Autowired
    private SettlementPrepayService settlementPrepayService;

    @Autowired
    private SettlementIncomeService settlementIncomeService;

    @Autowired
    private SettlementIncomeActivityService settlementIncomeActivityService;

    private List<SettlementCompany> companyList;

    private SettlementIncome settlementIncome;

    private SettlementPrepay settlementPrepay;

    private List<String> activityNameList;
    @PostConstruct
    public void init() throws Exception {
        companyList = settlementCompanyService.getCompanyList();
        settlementIncome = new SettlementIncome();
        settlementPrepay = new SettlementPrepay();
        activityNameList = Lists.newArrayList();
        initLazyDataModel();
    }

    private void initLazyDataModel() throws Exception {
        $ = new SettlementRemainRequest();
        lazyDataModel = new SettlementRemainLazyDataModel($);
    }

    public void onReset() throws Exception{
        initLazyDataModel();
    }

    public void onSeeRelateRecord(Long relateId,Integer remainType){
        if (remainType == RemainType.PREPAY.index){//预付
            settlementPrepay = settlementPrepayService.findOne(relateId);
        }
        else if (remainType == RemainType.EXPEND.index){//消耗
            settlementIncome = settlementIncomeService.findOne(relateId);
            activityNameList = settlementIncomeActivityService.findAllByIncomeId(relateId).parallelStream()
                    .map(s->s.getActivityName()).collect(Collectors.toList());
        }

    }

    @Override
    protected List<String> getDictionaryCodes() {
        return  Lists.newArrayList(SETTLEMENT_SPREAD_CHANNEL,SETTLEMENT_INCOME_TYPE, SETTLEMENT_SPREAD_MODE, COOPERATION_MODE, SETTLEMENT_CYCLE, SETTLEMENT_PAY_MODE);
    }
}
